package GestionThread;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author marc
 */
public class GestionThread extends Thread {

    private boolean aPause, start;
    private Runnable run;

    public GestionThread(Runnable r) {
        aPause = true;
        start = false;
        run = r;
    }

    @Override
    public synchronized void start() {
        if (!start) {
            start = true;
            aPause = false;
            new Thread(run).start();
        }
    }

    public void pause() {
        synchronized (this) {
            try {
                if(aPause)
                    wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(GestionThread.class.getName()).log(Level.SEVERE, null, ex);
            }
            aPause = false;
        }

    }

    public void reprendre() {
        synchronized (this) {
            if(aPause)
                notify();
        }
    }

    public boolean aMettreEnPause() {
        synchronized (this) {
            return aPause;
        }
    }

    public void mettreEnPause() {
        synchronized (this) {
            aPause = true;
        }
    }
    
    public boolean isStarted(){
        synchronized(this){
            return start;
        }
    }

}
